<script setup lang="ts">
import { ref, onMounted } from 'vue'
import axiosUtil from "../../axios-util/axios-util.ts";
import {ElMessage} from "element-plus";

const searchText = ref('')
const displayText = ref('')
const currentTextIndex = ref(0)
const texts = [
  '别着急，我来画帮你一键生成思维导图！',
  '各大平台的连接，发出来！'
]

const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))

const typeText = async (text: string) => {
  displayText.value = ''
  for (let char of text) {
    displayText.value += char
    await sleep(100) // 每个字的打字间隔
  }
  await sleep(2000) // 完整显示后的停留时间
}

const startTypeAnimation = async () => {
  while (true) {
    await typeText(texts[currentTextIndex.value])
    currentTextIndex.value = (currentTextIndex.value + 1) % texts.length
  }
}

const handleSearch = async () => {
  // TODO: 实现思维导图生成逻辑
  if(searchText.value){
    const response = await axiosUtil.post('api/opencz/url_to_siweidaotu', {
      url:searchText.value
    })
    if(response.data.code===1){
      //这里是成功的逻辑，但是目前没有成功，所以不知道怎么写
    }else{
      ElMessage.error(response.data.msg)
    }
  }else {
    ElMessage.error('请先输入分享链接')
  }
}

onMounted(() => {
  startTypeAnimation()
})
</script>

<template>
  <div class="mind-map-container">
    <div class="container">
      <div class="section-title">
        <h1 class="title">{{ displayText }}</h1>
      </div>
      <div class="search-box">
        <input
          type="text"
          v-model="searchText"
          placeholder="请输入内容生成思维导图"
          @keyup.enter="handleSearch"
        >
        <button class="search-btn" @click="handleSearch">✨ 提取</button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.mind-map-container {
  width: 100%;
  height: 100%;
  padding: 20px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
}

.container {
  display: flex;
  flex-direction: column;
  gap: 20px;
  align-items: center;
  justify-content: center;
  min-height: 100%;
}

.section-title {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 15px;
  min-height: 30px; /* 确保高度固定，防止文字切换时的抖动 */
}

.number-circle {
  width: 24px;
  height: 24px;
  background-color: #7c4dff;
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: bold;
}

.title {
  font-size: 32px;
  color: #333;
  margin: 0;
  font-weight: 600;
  white-space: nowrap; /* 防止文字换行 */
}

.search-box {
  display: flex;
  gap: 10px;
  width: 100%;
  max-width: 800px;
}

.search-box input {
  flex: 1;
  padding: 12px 20px;
  border: 1px solid #dcdfe6;
  border-radius: 8px;
  font-size: 16px;
  background-color: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  transition: all 0.3s;
}

.search-box input:hover {
  background-color: rgba(255, 255, 255, 0.2);
  border-color: #7c4dff;
}

.search-box input:focus {
  background-color: rgba(255, 255, 255, 0.2);
  border-color: #7c4dff;
  outline: none;
  box-shadow: 0 0 5px rgba(124, 77, 255, 0.2);
}

.search-btn {
  padding: 12px 24px;
  background-color: #7c4dff;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 16px;
  transition: all 0.3s;
  white-space: nowrap;
}

.search-btn:hover {
  background-color: #6c3fff;
  transform: translateY(-1px);
}
</style>
